package org.example.code.test2025.two_month;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class test0219 {
    public static List<Integer> maxWindow(int[] nums,int k){
        List<Integer> result = new ArrayList<>();
        if(nums == null||nums.length==0||k<=0||k> nums.length){
            return result;
        }
        Deque<Integer> deque = new LinkedList<>();
        for(int i=0;i<nums.length;i++){
            while(!deque.isEmpty()&&nums[deque.peekLast()]<=nums[i]){
                deque.pollLast();
            }
            deque.offerLast(i);
            if(deque.peekFirst()<=i-k){
                deque.pollFirst();
            }
            if(i>=k-1){
                result.add(nums[deque.peekFirst()]);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {2,3,4,2,6,2,5,1};
        int k = 3;
        System.out.println(maxWindow(nums,k));
    }
}
